package com.ly.lypopupwindowdemo.activity;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ViewUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.util.BitmapUtils;
import com.ly.lypopupwindowdemo.util.DirUtil;
import com.ly.lypopupwindowdemo.util.FileUtil;
import com.ly.lypopupwindowdemo.util.IntentUtil;
import com.ly.lypopupwindowdemo.util.LException;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;

public class GetPicActivity extends AppCompatActivity implements View.OnClickListener{
    private Context mContext;
    public static final String IMAGE_PATH = "image_path";
    public static final String IMAGE_CROP = "image_crop";
    private static final int REQ_CODE_CAMERA = 1;
    private static final int REQ_CODE_SELECT = 2;
    private static final int REQ_CODE_CROP = 3;
    private String mImagePath;
    private String mCropImagePath;
    private RelativeLayout mRootRl;
    private LinearLayout mBottomLl;
    private TextView mTakeTv;
    private TextView mSelectTv;
    private TextView mCancelTv;
    private boolean isNeedCrop = false;

    private static final String TAG = "GetPicActivity";



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_get_pic);

        mContext = this;
        initView();
        animIn();
        initTitle();
        initData();
    }

    protected void initView() {
        setContentView(R.layout.activity_get_pic);
        //com.lidroid.xutils.ViewUtils.inject(this);
        mRootRl = (RelativeLayout) findViewById(R.id.activity_getpic_root_rl);
        mBottomLl = (LinearLayout) findViewById(R.id.activity_getpic_bottom_ll);
        mTakeTv = (TextView) findViewById(R.id.activity_getpic_take_tv);
        mSelectTv = (TextView) findViewById(R.id.activity_getpic_select_tv);
        mCancelTv = (TextView) findViewById(R.id.activity_getpic_cancel_tv);
        mRootRl.setTag(0);
        mRootRl.setOnClickListener(this);
        mTakeTv.setTag(1);
        mTakeTv.setOnClickListener(this);
        mSelectTv.setTag(2);
        mSelectTv.setOnClickListener(this);
        mCancelTv.setTag(3);
        mCancelTv.setOnClickListener(this);
    }

    protected void animIn() {
        mBottomLl.startAnimation(AnimationUtils.loadAnimation(this, R.anim.anim_trans_in_b2t_300));
        overridePendingTransition(R.anim.anim_alpha_in_300, R.anim.anim_alpha_none_300);
    }

    @Override
    public void finish() {
        super.finish();
        animOut();
    }

    protected void animOut() {
//        mBottomLl.clearAnimation();
//        mBottomLl.startAnimation(AnimationUtils.loadAnimation(this,R.anim.anim_trans_out_t2b_300));
        overridePendingTransition(R.anim.anim_alpha_none_300, R.anim.anim_alpha_out_300);
    }

    protected void initTitle() {

    }

    protected void initData() {
        isNeedCrop = getIntent().getBooleanExtra(IMAGE_CROP, false);
    }

    @Override
    public void onClick(View v) {
        switch ((int) v.getTag()) {
            case 0:
            case 3:
                finish();
                break;
            case 1:
                onTake();
                break;
            case 2:
                onSelect();
                break;
        }
    }

    private void onSelect() {
        try {
            IntentUtil.startSystemSelectImage(this, REQ_CODE_SELECT);
        } catch (LException e) {
            e.printStackTrace();
        }
    }

    private void onTake() {
        mImagePath = DirUtil.getImageCachePath() + File.separator + FileUtil.generateImagename();
        File file = new File(mImagePath);
        try {
            IntentUtil.startSystemCamera(this, REQ_CODE_CAMERA, Uri.fromFile(file));
        } catch (LException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult: 打印数据==="+"requestCode=" + requestCode + ",resultCode=" + resultCode );
        if (data != null)
            Log.e(TAG, "onActivityResult: data数据==="+data );
        if (resultCode == RESULT_OK) {
            if (requestCode == REQ_CODE_CAMERA) {
                if (!isNeedCrop) {
                    setResultPath(mImagePath);
                    return;
                }
                String mCropImagePath = DirUtil.getImageCachePath() + File.separator + FileUtil.generateImagename();
                if ("GT-I9300".equals(android.os.Build.MODEL)) {
                    Bitmap bitmap = BitmapUtils.rotaingImage(
                            BitmapUtils.readPictureDegree(mImagePath),
                            BitmapUtils.getBitmapByPathAndScale(mImagePath, 10));
                    BitmapUtils.saveBitmap2File(bitmap, mCropImagePath);
                    setResultPath(mCropImagePath);
                } else {
                    try {
                        IntentUtil.startSystemCropImage(this, Uri.fromFile(new File(mImagePath)), REQ_CODE_CROP);
                    } catch (LException e) {
                        e.printStackTrace();
                        //"手机不支持裁剪"
                        Bitmap bitmap = BitmapUtils.rotaingImage(
                                BitmapUtils.readPictureDegree(mImagePath),
                                BitmapUtils.getBitmapByPathAndScale(mImagePath, 10));
                        BitmapUtils.saveBitmap2File(bitmap, mCropImagePath);
                        setResultPath(mCropImagePath);
                    }
                }
            } else if (requestCode == REQ_CODE_SELECT) {
                try {
                    if (!isNeedCrop) {
                        ContentResolver cr = this.getContentResolver();
                        Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(data.getData()));
                        String path = DirUtil.getImageCachePath() + File.separator + FileUtil.generateImagename();
                        BitmapUtils.saveBitmap2File(bitmap, path);
                        setResultPath(path);
                        return;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    IntentUtil.startSystemCropImage(this, data.getData(), REQ_CODE_CROP);
                } catch (LException e) {
                    e.printStackTrace();
                    //"手机不支持裁剪"
                    try {
                        ContentResolver cr = this.getContentResolver();
                        Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(data.getData()));
                        String path = DirUtil.getImageCachePath() + File.separator + FileUtil.generateImagename();
                        BitmapUtils.saveBitmap2File(bitmap, path);
                        setResultPath(path);
                    } catch (Exception e1) {
                        e1.printStackTrace();
                    }

                }

            } else if (requestCode == REQ_CODE_CROP) {
                if (data != null) {
                    Bundle extras = data.getExtras();
                    if (extras != null) {
                        Bitmap bitmap = extras.getParcelable("data");
                        String path = DirUtil.getImageCachePath() + File.separator + FileUtil.generateImagename();
                        BitmapUtils.saveBitmap2File(bitmap, path);
                        setResultPath(path);
                    }
                }
            }
        }
    }

    private void setResultPath(String path) {
        Intent intent = new Intent();
        intent.putExtra(IMAGE_PATH, path);
        setResult(RESULT_OK, intent);
        finish();
    }
}
